package com.ekang.admin.service;


import com.ekang.admin.dao.GoodsMapper;
import com.ekang.admin.dao.OrderMapper;
import com.ekang.admin.model.Goods;
import com.ekang.admin.model.Order;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.commons.lang3.time.FastDateFormat;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * Created by zcz on 2017/10/27.
 */
@Service
public class GoodsService {

    @Autowired
    private GoodsMapper goodsMapper;

    @Autowired
    private OrderMapper orderMapper;


    private static Log logger = LogFactory.getLog(GoodsService.class);


    public List<Goods> getGoodsList(String type) {
        return goodsMapper.getAllGoods();
    }


    public Goods getGoodsById(Integer id) {
        return goodsMapper.getGoodsById(id);
    }


    public Order getOrderById(Integer id) {
        return orderMapper.getOrderById(id);
    }


    public Order getOrderByOrderNO(String orderNO) {
        return orderMapper.getOrderbyOrderNO(orderNO);
    }


    public Order addOrder(Order order) {
        FastDateFormat fdf = FastDateFormat.getInstance("yyyyMMddHHmmss");
        String orderNO = fdf.format(System.currentTimeMillis()) + RandomStringUtils.randomNumeric(5);
        order.setStatus(0);
        order.setAddTime(System.currentTimeMillis());
        order.setOrderNO(orderNO);
        orderMapper.insertOrder(order);
        return orderMapper.getOrderbyOrderNO(orderNO);
    }


    public Order updateOrder(Order order) {
        orderMapper.updateOrderPayByNO(order);
        return order;
    }


    public Order updateOrderById(Order order) {
        orderMapper.updateOrderById(order);
        return order;
    }


    public Order updateOrderPayed(Order order) {
        orderMapper.updateOrderPayed(order);
        return order;
    }

    public Object getOrderListByUserId(String userId) {
        return orderMapper.getUserOrderList(userId);
    }

    public int updateGoodsState(String type, String state) {
        return goodsMapper.updateGoodsState(type, state);
    }


    public Object getGoodsListByCond(Map para) {
        return goodsMapper.getGoodsList(para);
    }


    public Object getOrderList(Map para) {
        return orderMapper.getOrderList();
    }


}
